package 寻找峰值;

class Solution {
    //暴力求解
    public int findPeakElement1(int[] nums) {
        if(nums.length == 1){
            return 0;
        }
        int ret = 0;
        for(int i = 1; i < nums.length - 1; i++){
            if(nums[i] > nums[i - 1] && nums[i] > nums[i + 1]){
                ret = i;
                break;
            }
        }
        if(ret == 0 && nums[ret] < nums[nums.length - 1]){
            return nums.length - 1;
        }
        if(ret == 0){
            return nums[0] > nums[1] ? 0 : 1;
        }
        return ret;
    }

    //利用二分查找算法进行求解
    public int findPeakElement(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        while(left < right){
            int mid = left + (right - left + 1) / 2;
            if(nums[mid] > nums[mid - 1]){
                left = mid;
            }else{
                right = mid - 1;
            }
        }
        return right;
    }
}